﻿using System.Text;
using RAP.Framework.Libary.DomTemplateBuilder.Handler.Conditional;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler.StringActions
{
	public abstract class StringActionConditionalElementHandler : StringActionElementHandler
    {
        public new IConditionalElement Element
        {
            get
            {
                return base.Element as IConditionalElement;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public ElseHandler ElseHandler
        {
            get
            {
                if ( this.Element.Else == null )
                {
                    return null;
                }
                return this.Element.Else.Handler as ElseHandler;
            }
        }

        public override void DoRender ( StringBuilder writer, object rootParamObject, object parentParamObject, ITemplateRenderContext context )
        {
            if ( this.IsCondition ( rootParamObject, parentParamObject, context ) )
            {
                base.DoRender ( writer, rootParamObject, parentParamObject, context );
            }
            else
            {
				var elseHandler = this.ElseHandler;
                if ( elseHandler != null )
                {
                    object paramObject = this.GetParamObject ( rootParamObject, parentParamObject, context );
                    elseHandler.DoRender ( writer, rootParamObject, paramObject, context );
                }
            }
        }

        public abstract bool IsCondition ( object rootParamObject, object parentParamObject, ITemplateRenderContext context );
    }
}
